<?php

namespace addons\cloud_market\controller\api;

use addons\cloud_market\model\CloudMarketLabel;

/**
 * 应用接口
 */
class Label extends CloudMarketApiController
{
    protected $notApiLogin = ['index','getLabels'];
    protected $notApiCheck = ['*'];

    /**
     * @var CloudMarketLabel
     */
    protected $model;

    protected function initialize()
    {
        parent::initialize();
        $this->model = new CloudMarketLabel();
    }

    /**
     * 首页
     */
    public function index()
    {
        return $this->success('请求成功');
    }

    /**
     * Create by :  刘泓宾<514660094@qq.com>
     * @param $type 1 应用 2 问答
     * @return \think\response\Json
     */
    public function getLabels($type = 2)
    {
        $lables = $this->model->where(['type'=>$type])->limit(0,10)->select();
        return $this->success('', $lables);
    }


    public function getLabelOptions()
    {
        $type = input('type',1);
        $name = input('name','');
        $where = [
            ['type','=',$type]
        ];
        if($name){
            $where[] = ['name','like',"%{$name}%"];
        }

        $res = $this->model->where($where)->field(['id', 'name'])->select();
        $data = [];
        foreach ($res as $value) {
            $data[] = [
                'value' => $value['id'],
                'label' => $value['name'],
            ];
        }
        return $this->success('', $data);
    }

}
